/**
 * Copyright (c) 2009 Jim Hurne
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter.gui;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

/**
 * JPanel that fills it's background with a Gradient.
 * 
 * @author Jim Hurne
 * @since Jul 27, 2009
 */
public class GradientPanel extends JPanel
{
  private static final long serialVersionUID = 1L;
  private BufferedImage bgImage;

  @Override
  protected void paintComponent(Graphics g)
  {
    //Graphics2D g2 = (Graphics2D) g;
    
    if(bgImage == null ||
        bgImage.getHeight() != this.getHeight())
    {
      //Create a compatible image
      GraphicsConfiguration config = GraphicsEnvironment
          .getLocalGraphicsEnvironment().getDefaultScreenDevice()
          .getDefaultConfiguration();
      bgImage = config.createCompatibleImage(1, this.getHeight());
      
      // Draw the gradient
      Graphics2D g2 = bgImage.createGraphics();
      GradientPaint paint = new GradientPaint(0, 0, Color.BLACK, 0, this.getHeight(), Color.GRAY);
      g2.setPaint(paint);
      
      g2.fillRect(0, 0, 1, this.getHeight());
      
      g2.dispose();
    }
    g.drawImage(
        bgImage, 0, 0, this.getWidth(), this.getHeight(), null);
  }
}
